﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.Common;

namespace Sinacor.Infra.Core.Licensing.Data
{
    public class ResourceAccess
    {
        protected delegate T ConstructLicensing<T>(IDataReader reader) where T : new();

        public ResourceAccess()
        {
        }

        protected IDbCommand GetSqlStringCommand(string comando)
        {
            System.Data.OracleClient.OracleConnection _connection = new System.Data.OracleClient.OracleConnection();
            IDbCommand cmd = _connection.CreateCommand();
            cmd.CommandText = comando;
            cmd.CommandType = CommandType.Text;
            cmd.Connection.ConnectionString = this.GetConnectionString();
            cmd.Connection.Open();
            return cmd;
        }

        protected void AddInParameter(IDbCommand cmd, string nameParameter, DbType ParameterParameter, object valueParameter)
        {
            IDbDataParameter parameter = cmd.CreateParameter();

            parameter.ParameterName = nameParameter;
            parameter.DbType = ParameterParameter;
            parameter.Direction = ParameterDirection.Input;

            if (valueParameter != null && !string.IsNullOrEmpty(valueParameter.ToString()))
                parameter.Value = valueParameter;
            else
                parameter.Value = DBNull.Value;

            cmd.Parameters.Add(parameter);
        }

        protected T FindOne<T>(IDbCommand command, ConstructLicensing<T> constructLicensing) where T : new()
        {
            T entity = default(T);

            using (IDataReader reader = command.ExecuteReader())
            {
                if (reader.Read())
                {
                    entity = constructLicensing(reader);
                }
            }

            command.Connection.Close();

            return entity;
        }

        protected List<T> FindAll<T>(IDbCommand command, ConstructLicensing<T> constructLicensing) where T : new()
        {
            T entity = default(T);
            List<T> entities = new List<T>();

            using (IDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    entity = constructLicensing(reader);
                    entities.Add(entity);
                }
            }

            command.Connection.Close();

            return entities;
        }

        protected int ExecuteNonQuery(IDbCommand command)
        {
            int result = command.ExecuteNonQuery();
            command.Connection.Close();
            return result;
        }

        protected object ExecuteScalar(IDbCommand command)
        {
            object result = command.ExecuteScalar();
            command.Connection.Close();
            return result;

        }

        protected string AddCharacterLike(string valueParameter)
        {
            string value = null;

            if (!string.IsNullOrEmpty(valueParameter) && !valueParameter.Contains('%'))
                return value = "%" + valueParameter.Replace(' ', '%') + "%";

            return value;
        }

        protected string GetConnectionString()
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings["ORACLETFWD"].ConnectionString;
        }
    }
}